package com.jstyle.beatecg.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.AlignSelf;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.util.ScreenUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/21.
 */

public class SportTypeAdapter extends RecyclerView.Adapter {
    String[]array;
    onItemClickListener clickListener;
    public  SportTypeAdapter(String[]array,onItemClickListener clickListener){
        this.clickListener=clickListener;
        this.array=array;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sport_type, parent, false);
        return new SportViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SportViewHolder sportViewHolder = (SportViewHolder) holder;
        Button button=sportViewHolder.sportType;
        Context context=button.getContext();
        FlexboxLayoutManager.LayoutParams lp = (FlexboxLayoutManager.LayoutParams) button.getLayoutParams();
        button.setText(array[position]);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 0||position==12||position==17)return;
                    clickListener.onItemClick(position);
            }
        });
        if (position == 0||position==12||position==17) {
            button.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
            button.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            //button.setWidth(ScreenUtils.getScreenWidth(holder.itemView.getContext()));
            lp.setFlexBasisPercent(1);
            lp.setMargins(ScreenUtils.dip2px(button.getContext(),16),0,0,0);
        }else{
            int id = context.getResources().getIdentifier("sport_" + position, "drawable", context.getPackageName());
            Drawable drawable= context.getResources().getDrawable(id);
            if(drawable!=null){
                drawable.setBounds(0,0 , drawable.getMinimumWidth(), drawable.getMinimumHeight());
                button.setCompoundDrawables(null,drawable,null,null);
            }
        }


    }


    @Override
    public int getItemCount() {
        return 20;
    }

    public class SportViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bt_sport_type_item)
        Button sportType;

        public SportViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public  interface onItemClickListener{
        public void onItemClick(int position);
    }
}
